####################################################################################
## Chromosome	Start_position	Tumor_Sample_Barcode	Reference_Allele	Tumor_Seq_Allele2	Hugo_Symbol	Variant_Classification	Variant_Type
## 整理的格式
head="Chromosome\tStart_position\tTumor_Sample_Barcode\tReference_Allele\tTumor_Seq_Allele2\tHugo_Symbol\tVariant_Classification\tVariant_Type\tt_alt_count\tt_ref_count\tTranscript_ID\tHGVSp_Short\tall_effects\tFrom" \

####################################################################################
## TCGA
echo -e ${head} > ${maf_public_path}/TCGA_use.addVAF.maf
cat ${work_dir}/public_ref/TCGA/TCGA_info_include_mut.tsv | grep -v Chromosome | \
awk -F'\t' '{OFS="\t"}{print $6,$7,$1,$12,$14,$2,$10,$11,$43,$42,$39,$38,$47,From}' From="TCGA" \
>> ${maf_public_path}/TCGA_use.addVAF.maf

####################################################################################
## Oncosg
echo -e ${head} > ${maf_public_path}/OncoSG_use.addVAF.maf
cat ${work_dir}/public_ref/OncoSG/OncoSG_info_include_mut.tsv | grep -v Chromosome | \
awk -F'\t' '{OFS="\t"}{print $5,$6,$17,$12,$14,$1,$10,$11,$35,$34,$41,$40,$46,From}' From="OncoSG" \
>> ${maf_public_path}/OncoSG_use.addVAF.maf

####################################################################################
## utokyo
echo -e ${head} > ${maf_public_path}/utokyo_use.addVAF.maf
cat ${work_dir}/public_ref/utokyo/utokyo_STAD.maf | grep -v Chromosome | \
awk -F'\t' '{OFS="\t"}{print $5,$6,$17,$12,$14,$1,$10,$11,$35,$34,$41,$40,"unknown",From}' From="Utokyo" \
>> ${maf_public_path}/Utokyo_use.addVAF.maf

####################################################################################
## TMUCIH
echo -e ${head} > ${maf_public_path}/TMUCIH_use.addVAF.maf
cat ${work_dir}/public_ref/TMUCIH/TMUCIH_STAD.maf | grep -v Chromosome | \
awk -F'\t' '{OFS="\t"}{print $5,$6,$17,$12,$14,$1,$10,$11,$35,$34,$41,$40,$92,From}' From="TMUCIH" \
>> ${maf_public_path}/TMUCIH_use.addVAF.maf

####################################################################################
## NMU
## cancer和IM
echo -e ${head} > ${maf_public_path}/NMU_use.addVAF.maf 
cat ${maf_path}/All_GGA.all.maf | grep -v Chromosome | \
awk -F'\t' '{OFS="\t"}{if($81 > 0)print $5,$6,$16,$11,$13,$1,$9,$10,$81,$82,$36,$42,$43,From}' From="NJMU" \
>> ${maf_public_path}/NMU_use.addVAF.maf

####################################################################################
## 合并
echo -e ${head} > ${maf_public_path}/All_raw.addVAF.maf
cat ${maf_public_path}/NMU_use.addVAF.maf ${maf_public_path}/OncoSG_use.addVAF.maf ${maf_public_path}/TCGA_use.addVAF.maf \
${maf_public_path}/Utokyo_use.addVAF.maf ${maf_public_path}/TMUCIH_use.addVAF.maf | \
grep -v Chromosome >> ${maf_public_path}/All_raw.addVAF.maf
raw_mutNum=`cat ${maf_public_path}/All_raw.addVAF.maf | grep -v Chromosome | wc -l`

## 去除性染色体突变、去除长度超过50的
cat ${maf_public_path}/All_raw.addVAF.maf | \
awk -F'\t' '{if((length($4)-length($5))^2<=(length_limit^2)){print}}' length_limit=50 |\
awk -F'\t' '{if($0~"Chromosome" || ($1!="X" && $1!="Y")){print}}' |\
awk -F'\t' '{OFS="\t"}{print $1,$2,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}' | grep -v Chromosome \
> ${maf_public_path}/All_raw.addVAF.QC1.maf
QC1_mutNum=`cat ${maf_public_path}/All_raw.addVAF.QC1.maf | grep -v Chromosome | wc -l`

## SimpleRepeats Region Delete
${bedtools} intersect -a ${maf_public_path}/All_raw.addVAF.QC1.maf -b ~/ref/SimpleRepeats/GRCh37_SimpleRepeats.bed -v -header \
> ${maf_public_path}/All_raw.addVAF.QC2.maf
QC2_mutNum=`cat ${maf_public_path}/All_raw.addVAF.QC2.maf | grep -v Chromosome | wc -l`

## Seg Dup Region
${bedtools} intersect -a ${maf_public_path}/All_raw.addVAF.QC2.maf -b ~/ref/SimpleRepeats/GRCh37_segdup.nochr.bed -v -header \
> ${maf_public_path}/All_raw.addVAF.QC3.maf
QC3_mutNum=`cat ${maf_public_path}/All_raw.addVAF.QC3.maf | grep -v Chromosome | wc -l`

## 最后使用的
echo -e ${head} > ${maf_public_path}/All_use.addVAF.maf
cat ${maf_public_path}/All_raw.addVAF.QC3.maf | awk -F'\t' '{OFS="\t"}{print $1,$2,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15}' | \
grep -v Chromosome >> ${maf_public_path}/All_use.addVAF.maf

echo RawMutNum","Sex_Length","SimpleRepeats_QC_num","SegDup_QC_num > ${maf_public_path}/Vcf_QC.addVAF.list
echo ${raw_mutNum}","${QC1_mutNum}","${QC2_mutNum}","${QC3_mutNum} >> ${maf_public_path}/Vcf_QC.addVAF.list
